﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using U3dUtils;
using UI00;

namespace G12 {
    public class UI_Main : UIBase {
        public static string prefabPath;

        protected override string _GetUIPrefabPath() {
            if (prefabPath != null) {
                return prefabPath;
            }
            return "Assets/ImgPreview/ResourcesAB/ImgPreview/UI/UI_Main/UI_Main.prefab";
        }

        public static Transform parent;

        protected override Transform _GetUIParent() {
            if (parent != null) {
                return parent;
            }
            return UIBase.uiLayer2_uiMain;
        }

        UI_Setup uiSetup {
            get {
                if (_uiSetup.IsNull()) {
                    _uiSetup = new UI_Setup();
                }
                return _uiSetup;
            }
        }
        UI_Setup _uiSetup;
        public CParameter parameter = new CParameter();
        Button setupButton;
        RectTransform selectBox, cardPrefab, boundary, load, showPathBox, previewCardFather;
        Text progress, showPathText;

        protected override void _Init() {
            setupButton = m_rootRt.GetControl<Button>("setupButton");
            selectBox = GetControl<RectTransform>("selectBox");
            cardPrefab = selectBox.GetControl<RectTransform>("cardPrefab");
            boundary = GetControl<RectTransform>("boundary");
            load = GetControl<RectTransform>("load");
            progress = load.GetControl<Text>("progress");
            showPathBox = GetControl<RectTransform>("showPathBox");
            previewCardFather = GetControl<RectTransform>("previewCardFather");
            showPathText = showPathBox.GetControl<Text>("text");
            m_rootRt.gameObject.SetActive(true);

            SetBoundary(boundary);
            setupButton.onClick.AddListener(delegate() {
                uiSetup.Open(null);
            });

            CSelectCard.InitObjectPool();
            CPreviewCard.InitObjectPool();
            CPreviewCard.objectPool.poolParent.SetParent(previewCardFather);

            CParameter.isShowAssetsPath.RegisterEvent("", delegate(long dd) {
                if (CParameter.isShowAssetsPath.value == 1) {
                    showPathBox.gameObject.SetActive(true);
                } else {
                    showPathBox.gameObject.SetActive(false);
                }
            });
            CParameter.isShowAssetsPath.value = 1;
        }

        protected override void _Open(Dictionary<string, object> paraDic) {
            StartRun();
        }

        void SetBoundary(RectTransform boundary) {
            boundary.GetChild(0).position = new Vector3(Screen.width / 2, Screen.height + 100, 0);
            boundary.GetChild(1).position = new Vector3(Screen.width + 100, Screen.height / 2, 0);
            boundary.GetChild(2).position = new Vector3(Screen.width / 2, -100, 0);
            boundary.GetChild(3).position = new Vector3(-100, Screen.height / 2, 0);
        }

        public void AssetsLoadProgress(float progress0) {
            if (progress0 < 1) {
                if (load.gameObject.activeSelf == false) {
                    load.gameObject.SetActive(true);
                }
                progress.text = $"场景正在加载中   {(progress0 * 100):00.0}%";
            } else {
                if (load.gameObject.activeSelf) {
                    load.gameObject.SetActive(false);
                }
            }
        }

        public void StartRun() {
            var sourcePath = Application.dataPath + "/../FilePackage";
            sourcePath = AppConfig.GetStr("ImgPreview_sourcePath", sourcePath);
            CAssetsLoad.GetInstance.GetAssets(sourcePath, delegate(List<Dictionary<string, FileData>> assetsPathDic) {
                Debug.LogErrorFormat("资源数量____" + assetsPathDic.Count);
                // C_Parameter.screenCardNumber = assetsPathDic.Count;
                ResetGame(assetsPathDic);
            });
        }

        float basiceCardSize;

        /// <summary>
        /// 重新开始生成卡片数据
        /// </summary>
        void ResetGame(List<Dictionary<string, FileData>> assetsPathDic) {
            int screenMaxNumber = assetsPathDic.Count + (int)(CParameter.selectCardMaxNumber.value * CParameter.selectRadiusZoomRatio.value * CParameter.selectRadiusZoomRatio.value * Mathf.PI);
            basiceCardSize = ((Screen.width * Screen.height) / screenMaxNumber) * 0.55f;
            basiceCardSize = Mathf.Pow(basiceCardSize, 0.5f);
            CCard.CParameter.basiceCardSize = basiceCardSize;
            cardPrefab.sizeDelta = new Vector2(1.2f, 0.75f) * basiceCardSize;
            if (cardFather != null) {
                GameObject.Destroy(cardFather.gameObject);
            }
            cardFather = new GameObject("cardFather02").transform;
            cardFather.SetParent(cardPrefab.parent);
            CCard.CloseData();
            UIBase.mono.StartCoroutine(InitSelectBox(assetsPathDic, cardPrefab));
        }

        Transform cardFather;

        IEnumerator InitSelectBox(List<Dictionary<string, FileData>> assetsPathList, RectTransform cardPrefab) {
            cardPrefab.gameObject.SetActive(false);
            float interval = 300 / basiceCardSize;
            var father = (RectTransform)cardPrefab.parent;
            int numberX = (int)(father.rect.width / (cardPrefab.rect.width + interval));
            int i = 0;
            var dic = assetsPathList.GetEnumerator();
            while (dic.MoveNext()) {
                var rect = RectTransform.Instantiate(cardPrefab, cardFather);
                rect.gameObject.SetActive(true);
                rect.localScale = Vector3.one;
                rect.anchorMin = new Vector2(0, 1);
                rect.anchorMax = new Vector2(0, 1);

                rect.GetComponent<RectTransform>().position = new Vector3((cardPrefab.sizeDelta.x + interval) * (0.5f + i % numberX), Screen.height - (cardPrefab.sizeDelta.y + interval) * (0.5f + i / numberX), 0);
                rect.sizeDelta = Vector2.one * basiceCardSize;

                var crad = rect.gameObject.AddComponent<CCard>();
                crad.Init(rect);
                crad.InitCard(rect, i);
                AssetsLoadProgress(i / (assetsPathList.Count + 0.01f));
                yield return UIBase.mono.StartCoroutine(crad.ILoad(dic.Current, basiceCardSize));
                i++;
            }
            yield return new WaitForSeconds(0);
            AssetsLoadProgress(2);
        }

        public void ShowAssetsPath(FileData file) {
            showPathText.text = file.assetsPath;
        }

        public class CParameter {
            /// <summary>
            /// 能选中的照片最大数量
            /// </summary>
            public static PLong selectCardMaxNumber = PLong.Get("selectCardMaxNumber", 4);
            /// <summary>
            /// 屏幕照片最大数量
            /// </summary>
            // public static int screenCardNumber = 800;
            /// <summary>
            /// 选中照片的放大比例
            /// </summary>
            public static PDouble selectRadiusZoomRatio = PDouble.Get("selectRadiusZoomRatio", 2.4f);
            /// <summary>
            /// 是否现在资源路径
            /// </summary>
            public static PLong isShowAssetsPath = PLong.Get("isShowAssetsPath", 1);
            /// <summary>
            /// 视频是否全部播放
            /// </summary>
            public static PLong videoIsAllPlay = PLong.Get("videoIsAllPlay", 0);
        }
    }
}
